package com.aaa.mrcai.controller;

import com.aaa.mrcai.pojo.vo.MineVO;
import com.aaa.mrcai.properties.OSSProperties;
import com.aaa.mrcai.result.AjaxResult;
import com.aaa.mrcai.service.MineService;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

/**
 * @author meowyf
 * @version 1.0
 * @Name Z_MJ
 * @date 2021/12/10 19:17:19
 */
@RestController
@RequestMapping("mine")
public class MineController {
    @Autowired
    private MineService mineService;
    @Autowired
    private OSSProperties ossProperties;

    @RequestMapping("/list/{uid}")
    public AjaxResult list(@PathVariable int uid){
      return mineService.list(uid);
    }

    @RequestMapping("updateMine")
    public AjaxResult updateMine(MineVO mineVO){
        return mineService.updateMine(mineVO);
    }

    @RequestMapping("uploadUicon")
    public AjaxResult uploadUicon(MineVO mineVO, MultipartFile pic) throws Exception{
        //1、判断格式、尺寸是否满足要求
        //2、审核阶段、机器鉴黄

        System.out.println(pic.getSize());
        System.out.println(pic.getContentType());
        System.out.println(pic.getInputStream());


// 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(ossProperties.getEndpoint(), ossProperties.getAccessKeyId(), ossProperties.getAccessKeySecret());

// 填写本地文件的完整路径。如果未指定本地路径，则默认从示例程序所属项目对应本地路径中上传文件流。
        long time = System.currentTimeMillis();
        String imgName= UUID.randomUUID().toString()+time+pic.getContentType().replace("image/",".");

// 依次填写Bucket名称（例如examplebucket）和Object完整路径（例如exampledir/exampleobject.txt）。Object完整路径中不能包含Bucket名称。
        ossClient.putObject(ossProperties.getBucket(), imgName, pic.getInputStream());
// 关闭OSSClient。
        ossClient.shutdown();
        //真实路径
        mineVO.setUicon(ossProperties.getRealUrl()+imgName);
        System.out.println(mineVO);
        return mineService.uploadBicon(mineVO);
    }
}
